package com.ngsky.ana.comm;

import com.ngsky.ana.index.HistoryList;
import com.ngsky.ana.index.ImageItem;
import com.ngsky.ana.index.ImageList;
import com.ngsky.ana.server.AnalyzeTask;
import com.ngsky.ana.utils.ImageUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import static com.ngsky.ana.conf.AppConstants.*;

/**
 * @Author syx
 * @Desc
 * @Create 2020/6/18 12:46
 */
public class AnaResList extends JDialog {

    private String fileDir;
    private ImageList imageList;
    private List<ImageItem> itemList;

    public AnaResList(Frame parent, String fileDir) {
        super(parent, "分析结果", true);
        setSize(WINDOW_W, WINDOW_H - 140);
        // 设置对话框大小不可改变
        setResizable(false);
        // 设置对话框相对显示的位置
        setLocationRelativeTo(this);

        this.fileDir = fileDir;
        if (null != fileDir && !"".equals(fileDir)) {
            File file = new File(fileDir);
            File[] files = file.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.endsWith(".jpg") || name.endsWith(".png") || name.endsWith(".gif");
                }
            });

            if (null != files && files.length > 0) {
                // 初始化首页滚动面板
                itemList = new ArrayList<ImageItem>();
                for (int i = 0; i < files.length; i++) {
                    File f = files[i];
                    ImageItem ii = new ImageItem(f.getPath());

                    itemList.add(ii);

                    // 启动新线程分析图片
                    Thread thread = new Thread(new AnalyzeTask(files.length, i, f.getPath()));
                    thread.start();
                }
                imageList = new ImageList(itemList);
                JScrollPane scrollPane = new JScrollPane(imageList, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                scrollPane.setPreferredSize(new Dimension(WINDOW_W, WINDOW_H));
                scrollPane.getVerticalScrollBar().setUnitIncrement(20);

                setContentPane(scrollPane);
                setVisible(true);
            }
        }
    }
}
